package cn.edu.scau.cmi.chenruilin.hibernate;

import java.util.Iterator;
import java.util.List;

import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;

//import cn.edu.scau.cmi.chenruilin.ioc.util.ApplicationContextUtil;

public class testClient {
	public static void main(String[] args) {
		Student s = new Student();
		s.setName("傻逼");
		s.setId(4L);

		newStudent(s);
		listStudents();
	}
		public static void newStudent(Student student) {
//			MajorDAO majorDAO=new MajorDAO();
			
			ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
			StudentDao studentDAO=(StudentDao) ac.getBean("studentDao");
			
//			要将数据保存到数据库，或者使用transaction，或者使用session的fulsh方法
			Transaction transaction = studentDAO.getSession().beginTransaction();
			studentDAO.save(student);
			transaction.commit();
//			(3) 或者不适用事务，使用flush从缓冲区中将数据持久化到数据库中
//			majorDAO.getSession().flush();
		}

	private static void listStudents() {
//		MajorDAO majorDAO=new MajorDAO();
		
		ApplicationContext ac = ApplicationContextUtil.getApplicationContext();
		StudentDao studentDAO=(StudentDao) ac.getBean("studentDao");
		List Students = studentDAO.findAll();
		Iterator<?> StudentIterator = Students.iterator();
		while(StudentIterator.hasNext()){
			Student Student = (Student) StudentIterator.next();
			System.out.println("调用DAO后的专业名是："+Student.getName()+"，调用DAO后的专业ID号是："+Student.getId());
		}
	}
}
